package com.xmut.xmutbackend.controller;

import com.xmut.xmutbackend.entity.Class;
import com.xmut.xmutbackend.entity.Result;
import com.xmut.xmutbackend.service.IClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/classes/list")
public class ClassController {

    @Autowired
    private IClassService classService;

    // 查询全部班级
    @GetMapping
    public Result<List<Class>> list() {
        List<Class> list = classService.list();
        return Result.success(list);
    }

    // 根据id查询班级
    @GetMapping("/{id}")
    public Result<Class> getById(@PathVariable Integer id) {
        Class cls = classService.getById(id);
        if (cls == null) {
            return Result.fail("班级不存在");
        }
        return Result.success(cls);
    }

    // 新增班级
    @PostMapping
    public Result<Class> save(@RequestBody Class cls) {
        boolean save = classService.save(cls);
        if (!save) {
            return Result.fail("新增班级失败");
        }
        return Result.success(cls);
    }

    // 更新班级
    @PutMapping
    public Result<Class> update(@RequestBody Class cls) {
        boolean update = classService.updateById(cls);
        if (!update) {
            return Result.fail("更新班级失败");
        }
        return Result.success(cls);
    }

    // 删除班级
    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Integer id) {
        boolean remove = classService.removeById(id);
        if (!remove) {
            return Result.fail("删除班级失败");
        }
        return Result.success();
    }
}
